<?php
require("cap.php");
require_once("classes/App.php");
$app = new App();

if((isset($_POST['show'])) and (isset($_POST['setDate'])) and ($_POST['tel'] !="#")
	and ($_POST['sYear'] >= "2000" and $_POST['sYear'] <="2019") 
	and ($_POST['sMonth'] >= "1" and $_POST['sMonth'] <="12") 
	and ($_POST['sDay'] >= "1" and $_POST['sDay'] <= "31")
	and ($_POST['sHour'] >= "0" and $_POST['sHour'] <= "23") 
	and ($_POST['sMinute'] >= "0" and $_POST['sMinute'] <= "59")){
	$conf = parse_ini_file("conf/trixbi.conf.php");
	$tpl = fread(fopen($conf['language'].".billing.tpl", "r"), filesize($conf['language'].".billing.tpl"));
	$tel = mysql_escape_string($_POST['tel']);
	$sYear = mysql_escape_string($_POST['sYear']);
	$sMonth = mysql_escape_string($_POST['sMonth']);
	$sDay = mysql_escape_string($_POST['sDay']);
	$sHour = mysql_escape_string($_POST['sHour']);
	$sMinute = mysql_escape_string($_POST['sMinute']);

	if($_POST['setDate'] == 0){
		$fYear = date("Y");
		$fMonth = date("m");
		$fDay = date("d");
		$fHour = date("H");
		$fMinute = date("i");
	}
	else if(($_POST['fYear'] >= "2000" and $_POST['fYear'] <="2019")
		and ($_POST['fMonth'] >= "1" and $_POST['fMonth'] <="12")
		and ($_POST['fDay'] >= "1" and $_POST['fDay'] <= "31")
		and ($_POST['fHour'] >= "0" and $_POST['fHour'] <= "23")
		and ($_POST['fMinute'] >= "0" and $_POST['fMinute'] <= "59")){
		$fYear = mysql_escape_string($_POST['fYear']);
		$fMonth = mysql_escape_string($_POST['fMonth']);
		$fDay = mysql_escape_string($_POST['fDay']);
		$fHour = mysql_escape_string($_POST['fHour']);
		$fMinute = mysql_escape_string($_POST['fMinute']);
	}
	else{ 
		echo '<b>'.$var_bill_error.'</b><br><a href="javascript:history.go(-1)"><-'.$var_back.'</a>';
		exit;
	}
	$count = 0;
	$price = $app->myQuery("SELECT * FROM `price`", "trixbi", TRUE);
	if(isset($price))
	foreach($price as $price){
		$pmask[$count] = $price['pmask'];
		$pid[$count] = $price['pid'];
		$pprice[$count] = $price['price'];
		$pline[$count] = $price['line'];
		++$count;
	}
	$qstr = "SELECT * FROM `cdr` WHERE ( `calldate`>='".$sYear."-".$sMonth."-".$sDay." ".$sHour.":".$sMinute.":00' and `calldate`<='".$fYear."-".$fMonth."-".$fDay." ".$fHour.":".$fMinute.":00' and `src`='".$tel."')";
	$query = $app->myQuery($qstr, "asteriskcdrdb", TRUE);
	$list = '<table width="100%">
			<tr bgcolor="#FFFCAD">
				<td id="body">#</td><td id="body">'.$var_date.'</td><td id="body">'.$var_direction.'</td><td id="body">'.$var_number.'</td><td id="body">'.$var_duration.'</td><td id="body">'.$var_price.'</td>
				</tr>';
	$further = '<table width="100%"><tr bgcolor="#FFFCAD"><td id="body">#</td><td id="body">'.$var_date.'</td><td id="body">'.$var_number.'</td><td id="body" align="right">'.$var_duration.'</td></tr>';
	$j = $k = 1;
	if($query)
	foreach($query as $query){
		$flag2 = TRUE;
		for($i=0; $i<$count; ++$i){
			if((eregi($pmask[$i], $query['dst'])) and ($pprice[$i] != 0)){
				if($flag == TRUE){
					$list =  $list.'<tr nowrap><td id="body">'.$j.'</td><td id="body" nowrap>'.$query['calldate'].'</td><td id="body" nowrap>'.$pline[$i].'</td><td id="body" nowrap>'.$query['dst'].'</td><td id="body" align="right" nowrap>'.(floor($query['billsec']/60)).':'.($query['billsec']%60).'</td><td id="body" align="right" nowrap>'.round($query['billsec']*$pprice[$i] / 60, 2).'</td></tr>';
					$sum = $sum + $query['billsec'] * $pprice[$i] / 60;
					++$j;
					$flag = FALSE;
				}
				else{
					$list =  $list.'<tr bgcolor="#E2E2E2"><td id="body" nowrap>'.$j.'</td><td id="body" nowrap>'.$query['calldate'].'</td><td id="body" nowrap>'.$pline[$i].'</td><td id="body" nowrap>'.$query['dst'].'</td><td id="body" align="right" nowrap>'.(floor($query['billsec']/60)).':'.($query['billsec']%60).'</td><td id="body" align="right" nowrap>'.round($query['billsec'] * $pprice[$i] / 60, 2).'</td></tr>';
					$sum = $sum + $query['billsec'] * $pprice[$i] / 60;
					++$j;
					$flag = TRUE;
				}
			}
			if(($flag2 == TRUE) and (!eregi($pmask[$i], $query['dst']))){
				$flag2 = TRUE;
			}
			else
				$flag2 = FALSE;
		}
		if($flag2)
			if($flag3){
				$further =  $further.'<tr><td id="body" nowrap>'.$k.'</td><td id="body" nowrap>'.$query['calldate'].'</td><td id="body" nowrap>'.$query['dst'].'</td><td id="body" align="right" nowrap>'.(floor($query['billsec']/60)).':'.($query['billsec']%60).'</td></tr>';
				++$k;
				$flag3 = FALSE;
			}
			else{
				$further =  $further.'<tr bgcolor="#E2E2E2"><td id="body" nowrap>'.$k.'</td><td id="body" nowrap id="body">'.$query['calldate'].'</td><td id="body" nowrap>'.$query['dst'].'</td><td id="body" align="right" nowrap>'.(floor($query['billsec']/60)).':'.($query['billsec']%60).'</td></tr>';
				++$k;
				$flag3 = TRUE;
			}
	}
	$list = $list.'<tr bgcolor="#FFD484"><td id="body">'.$var_total.':</td><td id="body" align="right" colspan="5">'.round($sum, 2).'</td></tr></table>';
	$further = $further.'</table>';
	$data = $app->myQuery("SELECT `user_name`, `user_details` FROM `users` WHERE `user_id`='".$_COOKIE['uid']."'", "trixbi", TRUE);
	$room = $app->myQuery("SELECT `name` FROM `users` WHERE `extension`='".$tel."'",
							"asterisk",
							TRUE);
	if(isset($room))
		foreach($room as $room);
	if(isset($data))
		foreach($data as $data);
	$data['user_details'] = str_replace("\r\n", "<br />", $data['user_details']);
	foreach($app->myQuery("SELECT `user_name` FROM `users` WHERE `user_id`='".$_COOKIE['uid']."'", "trixbi", TRUE) as $user);

	$tpl = str_replace("@title@", $var_title, $tpl);
	$tpl = str_replace("@bill_cap@", $var_bill_cap, $tpl);
	$tpl = str_replace("@num_tel@", $var_num_tel, $tpl);
	$tpl = str_replace("@subscriber@", $var_subscriber, $tpl);
	$tpl = str_replace("@basic_bill@", $var_basic_bill, $tpl);
	$tpl = str_replace("@unknown_bill@", $var_unknown_bill, $tpl);
	$tpl = str_replace("@admin_name@", $var_admin_name, $tpl);
	$tpl = str_replace("@print@", $var_print, $tpl);
	$tpl = str_replace("@cap_billing@", $var_cap_billing, $tpl);
	$tpl = str_replace("@menu@", $app->printMenu($var_main, $var_admin, $var_rates, $var_exit, TRUE, TRUE), $tpl);
	$tpl = str_replace("@logo@", $_COOKIE['uid'], $tpl);
	$tpl = str_replace("@details@", chop($data['user_details']), $tpl);
	$tpl = str_replace("@tel@", $tel, $tpl);
	$tpl = str_replace("@room@", $room['name'], $tpl);
	$tpl = str_replace("@sDate@",$sYear."-".$sMonth."-".$sDay." ".$sHour.":".$sMinute, $tpl);
	$tpl = str_replace("@fDate@", $fYear."-".$fMonth."-".$fDay." ".$fHour.":".$fMinute, $tpl);
	$tpl = str_replace("@list@", $list, $tpl);
	$tpl = str_replace("@further@", $further, $tpl);
	$tpl = str_replace("@user_name@", $data['user_name'], $tpl);
	echo $tpl;
}
else echo '<b>'.$var_bill_error.'</b><br><a href="javascript:history.go(-1)"><-'.$var_back.'</a>';
?>
